<script lang="tsx">
import { defineComponent, ref, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { getLivesDetailApi, getLivesLatestApi, updateLivesViewCountApi, type LivesDetailResponse } from '#/api/competition/lives';
import LatestNews from './latest-news.vue';
import NewsContent from './newsContent.vue';
import { $t } from '@vben/locales';
export default defineComponent({
  name: 'CompetitionLives',
  setup() {
    const route = useRoute();
    const router = useRouter();
    const loading = ref(false);
    const currentId = ref(route.params.id);
    const handleViewDetail = (data: any) => {
      currentId.value = data.id;
      loadLivesData();
      try {
        const params = {
          id: data.id
        }
        updateLivesViewCountApi(params).then((res) => {
          console.log('文章详情:', res);
        }).finally(() => {
          console.log('文章详情:', 'finally');
        });
      } catch (error) {}
      console.log('data', data);
    }

    // 模拟左侧资讯详情数据
    const newsDetailData = ref<LivesDetailResponse | null>(null);

    // 模拟右侧最新动态数据
    const latestDynamicsData = ref<any[]>([]);

    // 获取咨讯数据
    const loadLivesData = async () => {
      if (!currentId.value) {
        return;
      }
      loading.value = true;
      try {
        const response = await getLivesDetailApi(currentId.value as string);
        if (response && response) {
          newsDetailData.value = response;
        }
      } catch (error) {
        newsDetailData.value = null;
      } finally {
        loading.value = false;
      }
    };

    // 获取最新动态数据
    const loadLatestNews = async () => {
      console.log('route.params.compId', route.params.compId);
      console.log('route.params.groupId', route.params.groupId);
      if (!route.params.compId || !route.params.groupId) {
        return;
      }
      try {
        const response = await getLivesLatestApi({
          compId: route.params.compId as string,
          curPage: 1,
          pageSize: 5,
          module: '1',
          groupId: route.params.groupId as string,
          publishStatus: true
        });

        if (response && response.data && response.data.length > 0) {
          latestDynamicsData.value = response.data.map((item: any) => ({
            id: item.id,
            title: item.subject,
            date: item.publishTime
          }));
        }
      } catch (error) {
        console.error('加载最新动态失败:', error);
      }
    };

    onMounted(() => {
      loadLivesData();
      loadLatestNews();
    });

    return () => (
      <div class="competition-lives-container" v-loading={loading.value}>
        {/* 主要内容区域 */}

        <div class="main-content">
          <div class="mb-[12px] text-[12px] cursor-pointer">
            <span class="text-[#909399]" onClick={() => {
              router.go(-1);
            }}>{$t('competition.lives.competitionDynamic')}</span>
            <span class="text-[#191919]">/{$t('competition.lives.dynamicDetail')}</span>
          </div>
          <div class="content-wrapper">
            {/* 左侧全部动态 */}
            <NewsContent newsDetailData={newsDetailData.value as any} />
            {/* 右侧最新动态 */}
            <div class="right-column">
              <div class="section-header">
                <h2 class="text-[16px] text-[500] text-[#191919]">{$t('competition.lives.latestNews')}</h2>
              </div>
              <LatestNews latestDynamicsData={latestDynamicsData.value} handleViewDetail={handleViewDetail} />
            </div>
          </div>
        </div>
      </div>
    );
  }
})
</script>

<style scoped>
.competition-lives-container {
  min-height: 100vh;
  background: #f5f7fa;
}

/* 主要内容区域 */
.main-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 20px;
}

.content-wrapper {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 40px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid #e8eaed;
}

/* 右侧列样式 */
.right-column {
  background: white;
  border-radius: 16px;
  padding: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  height: fit-content;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .content-wrapper {
    grid-template-columns: 1fr;
    gap: 24px;
  }
}

@media (max-width: 768px) {
  .main-content {
    padding: 20px;
  }

  .left-column,
  .right-column {
    padding: 20px;
  }

  .section-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
  }
}
</style>
